#pragma strict

import System.Collections.Generic;

var obstacleTransformList : List.<GameObject>;
var obstacles : GameObject[];

private var scriptsGObj : GameObject;

private var BZ3UtilsComponent : BZ3Utils;

private var mass : int = 80;
private var drag : float = 0;
private var obstacleNameNumber : int;

private var spawnPositionObstacleObstacleY : float = 1.5;
private var spawnPositionObstacle : Vector3;
private var spawnRotationObstacle : Quaternion;

private var outOfRangeDistance : int = 500;
private var randomRangePos : float = 400;
private var randomRangeNeg : float = -400;
private var createDeadZoneNeg : float = -20;
private var createDeadZonePos : float = 20;

private var deadZoneNeg : float = -300;
private var deadZonePos : float = 300;

private var amount : int = 50;
private var minimalObstacleAmount : int = 20;

function Start(){
	scriptsGObj = GameObject.FindGameObjectWithTag("Scripts");
	
	BZ3UtilsComponent = scriptsGObj.GetComponent(BZ3Utils);
	
	CreateObstacles(scriptsGObj.transform);
}

function Update(){
	for(var obstacleTmp : GameObject in obstacleTransformList){
		var distance : float = Vector3.Distance(obstacleTmp.transform.position, scriptsGObj.transform.position);
//		Debug.Log("Name: " + obstacleTmp.name + " InstanceID: " + obstacleTmp.GetInstanceID + " Distance: " + distance);
		
		if(distance > outOfRangeDistance){
//			Debug.Log("Obstacle Distance " + distance);
//			Debug.Log("scriptsGObj.transform xyz" + scriptsGObj.transform.position.x + " " + scriptsGObj.transform.position.y + " " + scriptsGObj.transform.position.z);
			RepositionObstacle(obstacleTmp, scriptsGObj.transform);
		}
	}
}

//Called only at the beginning of the initialization.
public function CreateObstacles(trans : Transform){	
	for(var go : GameObject in obstacles){
		//TODO eg. 2qubes to 4 triangles and level/difficulty different obstacles.
		if(go.name.Equals("Qube")){
			obstacleBuilder(go,trans,8.8,8.8,8.8);
		}
		if(go.name.Equals("Pyramid")){
			obstacleBuilder(go,trans,3.7,3.7,3.7);
		}
	}
}

public function obstacleBuilder(go : GameObject, trans : Transform, sizeX : float, sizeY : float, sizeZ : float){
	for(var i=0; i<amount;i++){
	//Spawnposition in correlation with the playerposition
	spawnPositionObstacle = transform.TransformPoint(Vector3(trans.position.x + BZ3UtilsComponent.deadZoneRandomRange(randomRangeNeg, randomRangePos, createDeadZoneNeg, createDeadZonePos), spawnPositionObstacleObstacleY, trans.position.z + BZ3UtilsComponent.deadZoneRandomRange(randomRangeNeg, randomRangePos, createDeadZoneNeg, createDeadZonePos)));
	spawnRotationObstacle = transform.rotation;

	var goTMP : GameObject = Instantiate(go, spawnPositionObstacle, spawnRotationObstacle);
		goTMP.rigidbody.position = spawnPositionObstacle;
		goTMP.rigidbody.rotation = spawnRotationObstacle;
		goTMP.rigidbody.mass = mass;
		goTMP.rigidbody.drag = drag;
		goTMP.rigidbody.transform.localScale = Vector3(sizeX,sizeY,sizeZ); //Scales the go
		obstacleTransformList.Add(goTMP);
	}
}

public function RepositionObstacle(obstacle : GameObject, trans : Transform){
	obstacle.transform.position = Vector3(trans.position.x + BZ3UtilsComponent.deadZoneRandomRange(randomRangeNeg, randomRangePos, deadZoneNeg, deadZonePos), spawnPositionObstacleObstacleY, trans.position.z + BZ3UtilsComponent.deadZoneRandomRange(randomRangeNeg, randomRangePos, deadZoneNeg, deadZonePos));
	obstacle.transform.rotation = transform.rotation;
}

public function getObstacleAmount() : int{
	return amount;
}

public function setObstacleAmount(value : int){
	amount = value;
}

public function increaseObstacleAmount(value : int){
	amount += value;
}

public function decreaseObstacleAmount(value : int){
	if(amount<minimalObstacleAmount){
		amount -= value;
	}else{
		Debug.Log("Minimum of obstacle amount is reached: " + minimalObstacleAmount);
	}
}